package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.AuthDto;
import com.heima.user.entity.ApAuthor;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.entity.WmUser;
import com.heima.user.feign.ArticleClient;
import com.heima.user.feign.MediaClient;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-08-07
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    //按照状态查询用户认证列表
    public ResponseResult listByStatus(AuthDto dto) {
        //构造分页条件
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        //查询条件
        LambdaQueryWrapper<ApUserRealname> qw=new LambdaQueryWrapper<>();
        //根据状态进行查询
        if (dto.getStatus() !=null) {
            qw.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        Page<ApUserRealname> page1 = this.page(page, qw);
        //相应
        return new PageResponseResult(dto.getPage(),dto.getSize(),page1.getTotal(),page1.getRecords());
    }

    @Autowired
    private IApUserService userService;

    //审核通过/失败
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int i) {
        //根据id查询认证表数据
        ApUserRealname userRealname = this.getById(dto.getId());
        //失败，修改用户认证状态为2，保存失败原因
        if (i==0) {
            userRealname.setStatus(2);
            userRealname.setReason(dto.getReason());
            this.updateById(userRealname);
            return ResponseResult.okResult();
        }
        //审核成功
        //查询ap_user
        ApUser user = userService.getById(userRealname.getUserId());
        //创建自媒体账号,注册信息 ap_user同步
        WmUser wmUser=createWmUser(user);

        //创建作者账号
        if (wmUser!=null) {
            ApAuthor author=createAythor(user,wmUser);
            if (author!=null) {
                //认证表的状态修改为9,审核通过
                userRealname.setStatus(9);
                this.updateById(userRealname);
                //修改用户注册表的flag为1  自媒资人
                user.setFlag(1);
                userService.updateById(user);
            }
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    //创建文章作者
    private ApAuthor createAythor(ApUser user, WmUser wmUser) {
        //模拟异常
//        int a=1/0;
        //构建文章作者
        ApAuthor author=new ApAuthor();
        author.setName(user.getName());
        author.setType(2);
        author.setUserId(user.getId());
        author.setWmUserId(wmUser.getId());
        //远程调用文章服务接口
        ResponseResult<ApAuthor> result = articleClient.saveApAuthor(author);
        //判断操作成功
        if (result.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            //返回作者数据
            ApAuthor data = result.getData();
            //作者的id写回到自媒体用户表中
            wmUser.setApAuthorId(data.getId());
            mediaClient.updateWmUser(wmUser);
            return author;
        }else{
            throw new RuntimeException("创建文章作者失败");
        }
    }

    @Autowired
    private MediaClient mediaClient;

    @Autowired
    private ArticleClient articleClient;

    //创建自媒体用户
    private WmUser createWmUser(ApUser user) {
        //构建自媒体用户
        WmUser wmUser = new WmUser();
        //基本属性从ApUser复制
        BeanUtils.copyProperties(user,wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(user.getId());
        wmUser.setNickname(user.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
        //远程调用自媒体接口
        ResponseResult<WmUser> result = mediaClient.saveWmUser(wmUser);
        //判断调用的结果
        if(result.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
            WmUser data = result.getData();
            return data;
        }else{
            throw new RuntimeException("创建自媒体失败");
        }
    }
}
